// pages/Pet/PetHealthInfo/PetHealthInfo.js
//获取应用实例
const app = getApp();
const assetsAddress = app.globalData.global.assetsIp + '/' + app.globalData.global.assetsName;
import { queryPetInfo , queryPetHeaithInfo , updatePetHeaithInfo , updatePetVaccineRecord } from '../../../api/pet';
import { formatTime } from '../../../utils/util';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    assetsAddress: assetsAddress,
    petId: '',
    pet: {},
    healthList: [],
    vaccinateName: '', // 疫苗名称
    healthName: '', // 补充信息
    healthItem: {},
    currentDate: new Date().getTime(),
    minDate: new Date(2020,1,1).getTime(),
    maxDate: new Date().getTime(),
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    if(options.petId){
      this.setData({
        petId: options.petId,
      })
      this._queryPetInfo();
    }
    this._queryPetHeaithInfo();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {
    let userId = wx.getStorageSync('userId') ?? 10;
    return {
      path: '/pages/Welcome/Welcome?scene='+userId,
    }
  },
  // 查询宠物信息
  _queryPetInfo(){
    let _this = this;
    queryPetInfo({id: _this.data.petId,userId: wx.getStorageSync('userId')})
    .then(res => {
      if(res.success){
        if(res.data.content.length > 0){
          _this.setData({
            pet: res.data.content[0],
          })
        }
      }else{
        wx.showToast({
          title: res.errMessage,
          icon: 'none',
        })
      }
    })
  },
  // 查询宠物服务项目
  _queryPetHeaithInfo(){
    let _this = this;
    queryPetHeaithInfo({petId: _this.data.petId})
    .then(res => {
      if(res.success){
        _this.setData({
          healthList: res.data.content,
        })
      }else{
        wx.showToast({
          title: res.errMessage,
          icon: 'none',
        })
      }
    })
  },
  // 疫苗名称修改
  vaccinateNameChange(event){
    this.setData({
      vaccinateName: event.detail
    })
  },
  // 补充时间
  updateTime(e){
    let _this = this;
    const item = e.target.dataset.item||e.currentTarget.dataset.item;
    if(item.serviceProjectName == '疫苗'){
      if(_this.data.vaccinateName == ''){
        wx.showToast({
          title: '请输入疫苗类别',
          icon: 'none',
        });
        return;
      }
    }
    _this.setData({
      healthItem: JSON.parse(JSON.stringify(item)),
      showTime: true,
    })
  },
  // 确认选择时间
  dateConfirm(event){
    console.log(event)
    var time = formatTime(event.detail,'Y-M-D');
    if(this.data.healthItem.serviceProjectName == '疫苗'){
      this._updatePetVaccineRecord(time);
    }else{
      this._updatePetHeaithInfo(time);
    }
  },
  // 补充疫苗时间
  _updatePetVaccineRecord(time){
    let _this = this;
    updatePetVaccineRecord({petId: _this.data.petId,name: _this.data.vaccinateName,vaccinateTime: time})
    .then(res => {
      if(res.success){
        wx.showToast({
          title: '补充成功',
          icon: 'success'
        })
        _this._queryPetHeaithInfo();
        _this.onClose();
      }else{
        wx.showToast({
          title: res.errMessage,
          icon: 'none',
        })
      }
    })
  },
  // 修改宠物健康信息
  _updatePetHeaithInfo(time){
    let _this = this;
    updatePetHeaithInfo({petId: _this.data.petId,serviceProjectId: _this.data.healthItem.serviceProjectId,lastServiceTime: time,id: _this.data.healthItem.id})
    .then(res => {
      if(res.success){
        var message = '修改成功';
        if(_this.data.healthItem.lastServiceTime){
          message = '补充成功'
        }
        wx.showToast({
          title: message,
          icon: 'success'
        })
        _this._queryPetHeaithInfo();
        _this.onClose();
      }else{
        wx.showToast({
          title: res.errMessage,
          icon: 'none',
        })
      }
    })
  },
  onClose(){
    this.setData({
      showTime: false,
    })
  }
})